﻿using System;

namespace Homework3
{
    class Program
    {
        static void Main(string[] args)
        {
            double s=0,sum=0;
            string shapeInfo = "";
            bool iflegal=false;
            Random r = new Random();
            for(int i = 0; i < 10; i++)
            {
                Shape myShape=new Rectangle();
                switch (r.Next(1, 4))
                {
                    case 1: shapeInfo = "长方形"; myShape = ShapeFactory.createShape(shapeInfo,r.Next(1,100), r.Next(1, 100));break;
                    case 2: shapeInfo = "正方形"; myShape = ShapeFactory.createShape(shapeInfo, r.Next(1, 100)); break;
                    case 3: shapeInfo = "三角形"; myShape = ShapeFactory.createShape(shapeInfo, r.Next(1, 100), r.Next(1, 100), r.Next(1, 100)); break;
                }
                s = myShape.GetArea();
                sum += s;
                iflegal = myShape.IsLeg();
                Console.WriteLine($"第{i+1}个图形为{shapeInfo},面积为{s}，是否合法:{iflegal}");

            }
            Console.WriteLine($"面积和为{sum}");
            
        }
    }

    class ShapeFactory
    {
        public static Shape createShape(string name, params int[] size)
        {
            
            Shape s;
            switch (name)
            {
                case "长方形":s = new Rectangle(size[0],size[1]);
                    break;
                case "正方形":s = new Square(size[0]);
                    break;
                case "三角形":s = new Triangle(size[0],size[1],size[2]);
                    break;
                default:s = new Rectangle();break;
            }
            return s;

        }
    }

    abstract class Shape
    {
        abstract public double GetArea();
        abstract public bool IsLeg();
    }

    class Rectangle:Shape
    {
        public double Length;
        public double Width;

        public Rectangle()
        {
            this.Length = 5;
            this.Width = 10;
        }
        public Rectangle(double l,double w)
        {
            this.Length = l;
            this.Width = w;
        }

        public override double GetArea()
        {   if(IsLeg())
                return Length * Width;
            Console.WriteLine("This shape is illegal.");
            return 0;
        }

        public override bool IsLeg()
        {
            return Length * Width > 0 ? true:false;
        }
    }

    class Square : Rectangle
    {
        public Square() : base(5, 5) { }
        public Square(double a) : base(a, a) { }
        
        public new double GetArea()
        {   if(IsLeg())
                return Length * Width;
            Console.WriteLine("This shape is illegal.");
            return 0;
        }

        public new bool IsLeg()
        {
            return Length * Width > 0 ? true : false;
        }

    }
    
    class Triangle : Shape
    {
        readonly public double A,B,C;
        public Triangle() { A = B = C = 3; }
        public Triangle(double a1,double b1,double c1) { this.A = a1;this.B = b1;this.C = c1; }
        public override double GetArea()
        {
            if (IsLeg())
            {
                double p = (A + B + C) / 2;
                double s = Math.Sqrt(p * (p - A) * (p - B) * (p - C));
                return s;
            }

            Console.WriteLine("This shape is illegal.");
            return 0;
        }

        public override bool IsLeg()
        {
            return (A+B-C)*(B+C-A)*(A+C-B) > 0 ? true:false;
        }
    }
}
